package com.example.coreshopapp.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.example.coreshopapp.R;
import com.example.coreshopapp.adapter.BillPayAdapter;
import com.example.coreshopapp.adapter.ClassBillAdapter;
import com.example.coreshopapp.app.Constants;
import com.example.coreshopapp.base.BaseActivity;
import com.example.coreshopapp.bean.BillPayData;
import com.example.coreshopapp.bean.ClassBillData;
import com.example.coreshopapp.bean.ClassBillReq;
import com.example.coreshopapp.databinding.ActivityBillPayBinding;
import com.example.coreshopapp.task.BillPayTask;
import com.example.coreshopapp.task.ClassBillTask;
import com.example.coreshopapp.utils.FormDataUtils;
import com.example.coreshopapp.utils.LoadingUtils;
import com.example.coreshopapp.utils.TimeUtils;
import com.example.coreshopapp.weight.CustomDatePickerDialog;

import java.util.ArrayList;
import java.util.List;

public class BillPayActivity extends BaseActivity {
private ActivityBillPayBinding binding;
    private boolean isLoading = false;
    private ClassBillReq classBillReq = new ClassBillReq(1, 999);

    private List<BillPayData> billPayList = new ArrayList<>();
    private CustomDatePickerDialog startDialog;
    private ArrayList<String> timeObjects = new ArrayList<>();
    private BillPayAdapter billPayAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bill_pay);
        binding = ActivityBillPayBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        hideTopBar();
        initView();
        viewClick();
        getRequest();
    }

    private void initView() {
        billPayAdapter = new BillPayAdapter(billPayList);
        binding.billPayList.setAdapter(billPayAdapter);
    }

    private void viewClick() {
        binding.idTvStardate.setOnClickListener(view -> {
            // 起止日期
            checkTime("start");
        });
        binding.idTvEndDate.setOnClickListener(view -> {
            // 结束日期
            checkTime("end");
        });
        // 重置
        binding.btnReset.setOnClickListener(view -> {
            resetTime();
            classBillReq = new ClassBillReq(1, 999);
            classBillReq.setPaymentTime(timeObjects);
            binding.idEtInput.setText("");
            getClassBillList();
        });
        binding.btnSearch.setOnClickListener(view -> {
            classBillReq.setClassBillNo(binding.idEtInput.getText().toString());
            getClassBillList();
        });

    }

    private void getRequest() {
        resetTime();
        getClassBillList();
    }

    private void resetTime() {
        timeObjects.clear();
        String startTime = TimeUtils.getDateOffset(-30, "yyyy-MM-dd");
        timeObjects.add(startTime);
        timeObjects.add(TimeUtils.getCurrentTime().toString().substring(0, 10));
        classBillReq.setPaymentTime(timeObjects);
        binding.timeTv.setText(startTime);
        binding.timeTv.setVisibility(View.VISIBLE);

        binding.endTimeTv.setText(TimeUtils.getCurrentTime().toString().substring(0, 10));
        binding.endTimeTv.setVisibility(View.VISIBLE);
    }
    public void getClassBillList() {
        LoadingUtils.showProgressDialog(BillPayActivity.this, "温馨提示", "加载中，请稍后");
        List<Integer> objects = new ArrayList<>();
        if (Constants.AppConstVars.SnInfo != null) {
            objects.add(Constants.AppConstVars.SnInfo.getFItemID());
        }
        classBillReq.setPosId(objects);

        Log.d("getClassBillList: ",JSON.toJSONString(timeObjects));
        BillPayTask billPayTask = new BillPayTask(FormDataUtils.buildFormData(classBillReq));
        billPayTask.setHandler(billPayHandler);
        billPayTask.execute();
    }


    public void checkTime(String type) {
        startDialog = new CustomDatePickerDialog(this, 0, (startDatePicker, startYear, startMonthOfYear, startDayOfMonth) -> {
            classBillReq = new ClassBillReq(1, 999);
            String time = startYear + "-" + (startMonthOfYear + 1) + "-" + startDayOfMonth;
            if (type == "start") {
                if (!TimeUtils.startValidate(time, timeObjects.get(1))) {
                    showtoast(this, "起始日期不能大于结束日期");
                    return;
                }
                timeObjects.set(0, time);
                binding.timeTv.setText(time);
                binding.timeTv.setVisibility(View.VISIBLE);
            } else {
                if (!TimeUtils.beforeValidate(time)) {
                    showtoast(this, "结束日期不能大于当前日期");
                    return;
                }
                if (!TimeUtils.startValidate(timeObjects.get(0), time)) {
                    showtoast(this, "结束日期不能小于起始日期");
                    return;
                }
                timeObjects.set(1, time);
                binding.endTimeTv.setText(time);
                binding.endTimeTv.setVisibility(View.VISIBLE);
            }
            classBillReq.setPaymentTime(timeObjects);
        }, TimeUtils.getYear(), TimeUtils.getMonth() - 1, TimeUtils.getDay(), false);
        startDialog.show();
    }

    @SuppressLint("HandlerLeak")
    private Handler billPayHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            LoadingUtils.hideProgressDialog();
            switch (msg.what) {
                case 9900:
                    billPayList = JSON.parseArray(msg.obj.toString(), BillPayData.class);
                    binding.billPayList.setVisibility(billPayList.size() > 0 ? View.VISIBLE : View.GONE);
                    binding.formNone.setVisibility(billPayList.size() > 0 ? View.GONE : View.VISIBLE);
                    billPayAdapter.updateBillPayAdapter(billPayList);
                    Log.d( "handleMessage: ",JSON.toJSONString(billPayList));
                    break;
                case 9901:
                    Toast.makeText(BillPayActivity.this, "获取失败：" + msg.obj, Toast.LENGTH_LONG).show();
                    break;
                case 9902:
                    Toast.makeText(BillPayActivity.this, msg.obj.toString(), Toast.LENGTH_LONG).show();
                    break;
                case 9904:
                    gotoActivityFinish(LoginActivity.class);
                    Toast.makeText(BillPayActivity.this, msg.obj.toString(), Toast.LENGTH_LONG).show();
                    break;
            }
        }
    };
}